package com.songyl.controller;


import com.songyl.message.entity.Message;
import com.songyl.message.service.MessageService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.web.bind.annotation.*;
import com.songyl.common.ret.RetMsg;
import com.songyl.common.ret.RetTableMsg;

import javax.validation.Valid;
import java.util.List;

/**
 * @author syl
 */
@RestController
@RequestMapping("/message")
@CrossOrigin(origins = "*")
@EnableJpaAuditing
public class MessageController {

    @Autowired
    private MessageService messageService;

    @ApiOperation(value = "留言",notes = "留言保存操作")
    @PostMapping("/form")
    public RetMsg send(@ModelAttribute("form") Message message) {
        RetMsg retMsg = new RetMsg();
        if(messageService.send(message)){
            retMsg.setCode("1");
            retMsg.setData("留言成功~！");
        }else{
            retMsg.setCode("0");
            retMsg.setData("留言失败~！");
        }

        return retMsg;
    }
    @ApiOperation(value = "留言列表",notes = "留言列表")
    @GetMapping("/{flag}")
    public RetMsg getList(@Valid Message m) {
        List<Message> messages =messageService.findAll( m);
        RetTableMsg retMsg = new RetTableMsg();

        retMsg.setCode("10");
        retMsg.setMsg("hellp devtool ~!");
        retMsg.setCount(messages.size()+"条~!");
        retMsg.setData(messages);

        return retMsg;
    }
    @ApiOperation(value = "留言列表",notes = "留言列表")
    @GetMapping("/content/{id}/{flag}")
    public RetMsg get(@Valid Message message) {
        //这里有个坑 flag 默认值为0
        List<Message> messages =messageService.findAll( message);

        RetTableMsg retMsg = new RetTableMsg();
        retMsg.setCount(messages.size()+"条~!");
        retMsg.setData(messages);

        return retMsg;
    }

    /**
     * 根据主键id集合字符串批量删除
     */
    @ApiOperation(value = "删除留言",notes = "删除留言")
    @DeleteMapping("/{id}")
    public RetMsg delete(@PathVariable("ids") String  ids) {
        messageService.delete(ids);
        RetTableMsg retMsg = new RetTableMsg();
        retMsg.setCode("0");
        return retMsg;
    }
}
